package com.zhida.smartcontrol.pagectrl.lampmanager.singlelamp;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jay.tools.recycler.MultipleViewHolder;
import com.jay.tools.recycler.SingleRecyclerAdapter;
import com.jay.tools.ui.ButtonStyle;
import com.jay.tools.ui.Cbox;
import com.jay.tools.utils.ResUtil;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.common.bean.LampSwitchBean;
import com.zhida.smartcontrol.utils.MyUtils;

import java.util.List;

public class SingleLampAdapter extends SingleRecyclerAdapter<LampSwitchBean> {
    private int mColorGreen, mColorRed;

    public SingleLampAdapter(List<LampSwitchBean> data) {
        super(R.layout.item_lamp_single, data);
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        Context context = recyclerView.getContext();
        mColorGreen = ResUtil.getColor(context, R.color.c_22D49C);
        mColorRed = ResUtil.getColor(context, R.color.c_FF4D62);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void convert(MultipleViewHolder holder, LampSwitchBean info) {
        LinearLayout llBright = holder.getView(R.id.ll_bright);
        Cbox cbox = holder.getView(R.id.cbox);
        TextView tvType = holder.getView(R.id.tv_type);
        TextView tvDetail = holder.getView(R.id.tv_single_detail);
        TextView tvAddress = holder.getView(R.id.tv_address);
        TextView tvControl = holder.getView(R.id.tv_control);
        TextView tvBrightness = holder.getView(R.id.tv_brightness);
        TextView tvJiaoshi = holder.getView(R.id.tv_jiao_shi);
        TextView tvTime = holder.getView(R.id.tv_time);
        ButtonStyle btnEdit = holder.getView(R.id.btn_edit);
        ButtonStyle btnDel = holder.getView(R.id.btn_del); 
        ImageView iv_lamp_light=holder.getView(R.id.iv_lamp_light);

        cbox.setText(info.getImei());

        tvJiaoshi.setText("校时：" + info.getClockUpdateTime());
        if (info.getUpdateTimeFlag().equals("0")) {
            // 显示绿色
            tvJiaoshi.setTextColor(mColorGreen);
        } else {
            tvJiaoshi.setTextColor(mColorRed);
        }

        tvTime.setText("更新：" + info.getUpdateDate());
        if (info.getUpdateStatus().equals("0")) {
            // 绿色
            tvTime.setTextColor(mColorGreen);
        } else {
            tvTime.setTextColor(mColorRed);
        }

        String type;
        int sType = Integer.parseInt(info.getSwitchType());
        switch (sType) {
            case Const.NB_SINGLE:
                type = "N";
                break;
            case Const.PLC_SINGLE:
                type = "P";
                break;
            default:
                type = "Z";
                break;
        }
        tvType.setText(type);
        tvAddress.setText(info.getAddress());
        tvControl.setText(info.getControlType().equals(Const.AUTO) ? "自" : "手");

        String brightNess = info.getBrightNess();
        if (TextUtils.isEmpty(brightNess)) {
            llBright.setVisibility(View.GONE);
        }else if (brightNess.equals("0")){
            llBright.setVisibility(View.VISIBLE);
            tvBrightness.setText(MyUtils.getBrightNess(brightNess));
            iv_lamp_light.setImageResource(R.drawable.ic_lamp_light_guan);
        }else {
            llBright.setVisibility(View.VISIBLE);
            tvBrightness.setText(MyUtils.getBrightNess(brightNess));
            iv_lamp_light.setImageResource(R.drawable.ic_lamp_light);
        }

        cbox.setChecked(info.isCheck());
        cbox.setOnCboxClickListener(new Cbox.OnCboxClickListener() {
            @Override
            public void onCboxClick(View v) {
                info.setCheck(!info.isCheck());
            }
        });

        btnEdit.setOnClickListener(new MyClick(info, 0));
        btnDel.setOnClickListener(new MyClick(info, 1));
        tvDetail.setOnClickListener(new MyClick(info, 2));
    }

    private class MyClick implements View.OnClickListener {
        private LampSwitchBean mBean;
        private int tag;

        MyClick(LampSwitchBean bean, int tag) {
            this.mBean = bean;
            this.tag = tag;
        }

        @Override
        public void onClick(View v) {
            if (null != mListener) {
                mListener.onClick(mBean, this.tag);
            }
        }
    }

}
